<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">id</label>
        <el-input v-model="query.id" clearable placeholder="id" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">姓名</label>
        <el-input v-model="query.name" clearable placeholder="姓名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">手机号</label>
        <el-input v-model="query.mobile" clearable placeholder="手机号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">createdAt</label>
        <el-input v-model="query.createdAt" clearable placeholder="createdAt" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">updatedAt</label>
        <el-input v-model="query.updatedAt" clearable placeholder="updatedAt" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">token</label>
        <el-input v-model="query.token" clearable placeholder="token" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">工号</label>
        <el-input v-model="query.no" clearable placeholder="工号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">职位</label>
        <el-input v-model="query.position" clearable placeholder="职位" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">密码</label>
        <el-input v-model="query.password" clearable placeholder="密码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">用户上一次登录时间</label>
        <el-input v-model="query.lastestLoginAt" clearable placeholder="用户上一次登录时间" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">备注</label>
        <el-input v-model="query.note" clearable placeholder="备注" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">是否禁用</label>
        <el-input v-model="query.isForbidden" clearable placeholder="是否禁用" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">是否删除</label>
        <el-input v-model="query.isDeleted" clearable placeholder="是否删除" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">一卡通</label>
        <el-input v-model="query.cardId" clearable placeholder="一卡通" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">审核人id</label>
        <el-input v-model="query.auditorId" clearable placeholder="审核人id" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">审核日期</label>
        <el-input v-model="query.auditTime" clearable placeholder="审核日期" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">审核状态</label>
        <el-input v-model="query.status" clearable placeholder="审核状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="form.mobile" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="工号">
            <el-input v-model="form.no" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="职位">
            <el-input v-model="form.position" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.note" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="一卡通">
            <el-input v-model="form.cardId" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="mobile" label="手机号" />
        <el-table-column prop="no" label="工号" />
        <el-table-column prop="position" label="职位" />
        <el-table-column prop="lastestLoginAt" label="用户上一次登录时间" />
        <el-table-column prop="note" label="备注" />
        <el-table-column prop="isForbidden" label="是否禁用" />
        <el-table-column prop="cardId" label="一卡通" />
        <el-table-column prop="auditorId" label="审核人id" />
        <el-table-column prop="auditTime" label="审核日期" />
        <el-table-column prop="status" label="审核状态" />
        <el-table-column v-if="checkPer(['admin','user:edit','user:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudUser from '@/api/user'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, name: null, mobile: null, createdAt: null, updatedAt: null, token: null, no: null, position: null, password: null, lastestLoginAt: null, note: null, isForbidden: 0, isDeleted: null, cardId: null, salt: null, auditorId: null, auditTime: null, status: null }
export default {
  name: 'User',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '用户', url: 'api/user', idField: 'id', sort: 'id,desc', crudMethod: { ...crudUser }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'user:add'],
        edit: ['admin', 'user:edit'],
        del: ['admin', 'user:del']
      },
      rules: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'id' },
        { key: 'name', display_name: '姓名' },
        { key: 'mobile', display_name: '手机号' },
        { key: 'createdAt', display_name: 'createdAt' },
        { key: 'updatedAt', display_name: 'updatedAt' },
        { key: 'token', display_name: 'token' },
        { key: 'no', display_name: '工号' },
        { key: 'position', display_name: '职位' },
        { key: 'password', display_name: '密码' },
        { key: 'lastestLoginAt', display_name: '用户上一次登录时间' },
        { key: 'note', display_name: '备注' },
        { key: 'isForbidden', display_name: '是否禁用' },
        { key: 'isDeleted', display_name: '是否删除' },
        { key: 'cardId', display_name: '一卡通' },
        { key: 'auditorId', display_name: '审核人id' },
        { key: 'auditTime', display_name: '审核日期' },
        { key: 'status', display_name: '审核状态' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
  }
}
</script>

<style scoped>

</style>
